package com.qf.controller;

import com.qf.pojo.BaseResp;
import com.qf.pojo.Game;
import com.qf.service.GameService;
import com.qf.utils.UploadToken;
import com.qf.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;
import java.util.Map;

@RestController
@RequestMapping("/game")
public class GameController {

    @Autowired
    private GameService gameService;

    @PostMapping("/add")
    public BaseResp add(@RequestBody Game game) {
        return gameService.add(game);
    }

    @PostMapping("/del")
    public BaseResp del(@RequestBody Map map) {
        return gameService.del((Integer) map.get("id"));
    }

    @RequestMapping("/findAll")
    public BaseResp findAll(@RequestBody Map map) {
        return gameService.selectAll((Integer)map.get("page"),(Integer)map.get("size"));
    }

    @PostMapping("/upload")
    public BaseResp upload(@RequestParam("file") MultipartFile multipartFile){
        String upload = UploadUtils.upload(multipartFile);
        // System.out.println(map);
        BaseResp baseResp = new BaseResp();
        baseResp.setData(upload);
        return baseResp;
    }

    @GetMapping("/gettoken")
    public BaseResp gettoken(){
        String token = UploadToken.upload();
        BaseResp baseResp = new BaseResp();
        baseResp.setData(token);
        return baseResp;
    }
}
